<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/6
 * Time: 10:06
 */

namespace app\admin\model\user;

/*********** 专家 *************/

use app\common\model\Category;
use fast\Tree;
use think\Model;

class UserExperts extends Model
{
    protected $table = 'fa_user_experts';
    protected $auth;
    protected $model;

    public function __construct($data = [])
    {
        $this->auth = \app\common\library\Auth::instance();
        parent::__construct($data);
    }

    public function add($params = [])
    {
        if ($this->isUidExist()) {
            $this->u_age = $params['age'] ?? 0;
            $this->u_practitioner_year = $params['practitioner_year'] ?? 0;
            $this->u_status = 0;
            $this->u_card_front = $params['card_front'];
            $this->u_card_verso = $params['card_verso'];
            $this->u_update_time = date('Y-m-d H:i:s');
            $this->sex = $params['sex'];
            $this->birthday = $params["birthday"];
            $this->u_address = $params['u_address'];
            $this->u_identity_card = $params['u_identity_card'];
            $this->u_title = $params['u_title'];
            try {
                $this->save($this->getData(), ['u_uid' => $this->auth->id]);
                return '成功';
            } catch (\Exception $exception) {
                return '请重试';
            }

        } else {
            $this->setAttr('u_uid', $this->auth->id);
            $this->setAttr('u_create_time', date('Y-m-d H:i:s'));
            $this->setAttr('u_age', $params['age'] ?? 0);
            $this->setAttr('u_practitioner_year', $params['practitioner_year'] ?? 0);
            $this->setAttr('u_status', '0');
            $this->setAttr('u_card_front', $params['card_front']);
            $this->setAttr('u_card_verso', $params['card_front']);
            $this->setAttr('u_update_time', date('Y-m-d H:i:s'));
            $this->setAttr('sex', $params['sex']);
            $this->setAttr('birthday', $params["birthday"]);
            $this->setAttr('u_address', $params['u_address']);
            $this->setAttr('u_identity_card', $params['u_identity_card']);
            $this->setAttr('u_title', $params['u_title']);
            $this->save();
            return '成功';
        }
    }

    public function isUidExist()
    {
        return $this->where('u_uid', $this->auth->id)->find();
    }

    /**
     * 获取分类下的专家信息
     * */

    public function cIdExpertsList($where = [],$order = 'e.u_uid desc', $page = 1,$pageCount = 10)
    {

        $where = array_merge($where, ['e.u_status' => '1']);
        $page = ($page * $pageCount) - $pageCount;
        $list = $this->alias('e')
            ->field('e.u_uid experts_id,ep.sound_price,ec.c_id,ec.pid,u.nickname,u.avatar')
            ->join('fa_user_experts_price ep', 'e.u_uid = ep.uid')
            ->join('fa_user_experts_category ec ', 'ec.uid = e.u_uid')
            ->join('fa_user u', 'u.id = e.u_uid')
            ->limit($page, $pageCount)
            ->where($where)
            ->order($order)
            ->select();
        $category = new Category();
        $ueq = new UserExpertsQualification();

        if ($list) {
            foreach ($list as $key => $value) {
                //分类
                $cid = $value['pid'];
                $uid = $value['experts_id'];
                $user_category = $category->where('id', 'in', function ($query) use ($cid, $uid) {
                    $query->table('fa_user_experts_category')->field('c_id id')->where('pid', $cid)->where('uid', $uid);
                })->field('name,type,flag')->select();
                if ($user_category) {
                    $user_category = collection($user_category)->toArray();
                    $category_str = '';
                    foreach ($user_category as $c_k => $c_v) {
                        $category_str .= '#' . $c_v['name'] . ' ';
                    }
                    $list[$key]['user_category'] = $category_str;
                } else {
                    $list[$key]['user_category'] = '';
                }
                //证书
                $z_list = $ueq->where('uid', $uid)->field('name')->select();
                $z_str = '';
                if ($z_list) {
                    foreach ($z_list as $z_k => $z_v) {
                        if ($z_k < 2) {
                            $z_str .= ' ' . $z_v['name'];
                        }
                    }
                }
                $list[$key]['user_qualification'] = $z_str;
            }
            return collection($list)->toArray();
        } else {
            $list = [];
            return $list;
        }

    }

    //专家详情
    public function detail($uid)
    {
        $list = $this->alias('e')
            ->field('e.u_intro,e.u_practitioner_year,e.send_word,e.u_uid,ep.sound_price,ep.video_price,ep.face_price,ec.c_id,ec.pid,u.nickname,u.avatar,ed.crowd,ed.duration_time,ed.therapy,ed.style,ed.appointment_time')
            ->join('fa_user_experts_price ep', 'e.u_uid = ep.uid')
            ->join('fa_user_experts_category ec ', 'ec.uid = e.u_uid')
            ->join('fa_user u', 'u.id = e.u_uid')
            ->join('fa_user_experts_describe ed','ed.uid = e.u_uid')
            ->limit(0, 10)
            ->where(['e.u_uid' => $uid, 'e.u_status' => '1'])
            ->find();
        $category = new Category();
        $ueq = new UserExpertsQualification();

        if ($list) {

            //分类
            $pid = $list['pid'];
            $uid = $list['u_uid'];
            $user_category = $category->where('id', 'in', function ($query) use ($pid, $uid) {
                $query->table('fa_user_experts_category')->field('c_id id')->where('pid', $pid)->where('uid', $uid);
            })->field('name,type,flag')->select();
            if ($user_category) {
                $user_category = collection($user_category)->toArray();
                $category_str = '';
                foreach ($user_category as $c_k => $c_v) {
                    $category_str .= '#' . $c_v['name'] . ' ';
                }
                $list['user_category'] = $category_str;
            } else {
                $list['user_category'] = '';
            }
            //证书
            $z_list = $ueq->where('uid', $uid)->field('name')->select();
            $z_str = '';
            if ($z_list) {
                foreach ($z_list as $z_k => $z_v) {
                    if ($z_k < 2) {
                        $z_str .= ' ' . $z_v['name'];
                    }
                }
            }
            $list['user_qualification'] = $z_str;

            //证书列表 array 格式
            $z_arr = [];
            if ($z_list) {
                foreach ($z_list as $z_k => $z_v) {
                        $z_arr[] = $z_v['name'];
                }
            }
            $list['user_qualification'] = $z_str;
            $list['user_qualification_arr'] = $z_arr;

            $user_categoryArr = $category->where('id', 'in', function ($query) use ( $uid) {
                $query->table('fa_user_experts_category')->field('c_id id')->where('uid', $uid);
            })->field('name,type,flag,id,pid')->select();
            if($user_categoryArr){
                $tree = Tree::instance();
                $tree->init(\collection($user_categoryArr)->toArray(),'pid');
                $list['user_category_arr'] = $tree->getTreeArray(0);
            }else{
                $tree = Tree::instance();
                $tree->init(\collection($user_categoryArr)->toArray(),'pid');
                $list['user_category_arr'] = [];
            }

            //培训经历
            $uet = new UserExpertsTrain();
            $trainList =  $uet->where('uid',$list['u_uid'])->field('datetime train_datetime , detail train_detail')->select();
            $list['train_list'] = $trainList;
            return $list->toArray();
        } else {
            $list = [];
            return $list;
        }
    }

}